package chapter04.part01;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 主机名和IP地址转换
 * 
 * @author insight
 * @since 2021/8/29
 */
public class InetAddressTest {
    public static void main(String[] args) throws UnknownHostException {
        // 获取主机名对应的所有 IP 地址
        try (Scanner in = new Scanner(System.in)) {
            String host = in.nextLine();
            InetAddress[] allByName = InetAddress.getAllByName(host);
            for (InetAddress add : allByName) {
                System.out.println(add);
            }
        }
        
        // 其他程序获取 localhost
        // 因为其他程序无法用 127.0.0.1 来连接本地主机
        System.out.println(InetAddress.getLocalHost());
    }
}